// const filterText = oldText => {
//   if (oldText.length > 7) return oldText.substring(0, 5) + '...'
// }
export const columnsOfAlarm = vm => {
  return [
    {
      title: '设备名称',
      align: 'center',
      key: 'deviceName'
    },
    {
      title: '设备ID',
      align: 'center',
      key: 'deviceNo'
    },
    {
      title: '设备位置',
      align: 'center',
      key: 'location'
    },
    {
      title: '所属企业',
      align: 'center',
      key: 'company'
    },
    {
      title: '事件详情',
      align: 'center',
      render: (h, params) => {
        const content = params.row.content
        return h('span', content)
      }
    },
    {
      title: '是否恢复',
      align: 'center',
      render: (h, params) => {
        const { status } = params.row
        if (status == 1) return h('span', '已恢复')
        else if (status == 2) return h('span', '未恢复')
        else h('span', '-')
      }
    },
    {
      title: '事件时间',
      align: 'center',
      render: (h, params) => {
        const { happenDate } = params.row
        return h('span', vm.moment(happenDate).format('YYYY-MM-DD HH:mm:ss'))
      }
    },
    {
      title: '恢复时间',
      align: 'center',
      render: (h, params) => {
        const { recoverDate } = params.row
        if (!recoverDate) return h('spna', '-')
        else
          return h('span', vm.moment(recoverDate).format('YYYY-MM-DD HH:mm:ss'))
      }
    }
  ]
}

export const buildColumns = vm => {
  return [
    {
      align: 'center',
      title: '是否在线',
      key: 'online',
      render: (h, params) => {
        let row = params.row
        return filterOnline(h, row)
      }
    },
    {
      align: 'center',
      title: '状态',
      key: 'status',
      render: (h, params) => {
        return filterStatus(h, { ...params.row })
      }
    },
    {
      align: 'center',
      title: '设备ID',
      key: 'deviceNo'
    },
    {
      align: 'center',
      title: '设备名称',
      key: 'name'
    },
    {
      title: '网络状态',
      key: 'network',
      align: 'center'
    },
    {
      align: 'center',
      title: '设备类型',
      render: (h, params) => {
        let config = {
          1: '硬盘录像机',
          2: '热成像仪'
        }
        return h('span', config[params.row.type])
      }
    },
    {
      align: 'center',
      title: '设备位置',
      key: 'location'
    },
    {
      align: 'center',
      title: '所属企业',
      key: 'company'
    },
    {
      align: 'center',
      title: '创建时间',
      render: (h, params) => {
        const time = params.row.createdAt
        return h('span', vm.moment(time).format('YYYY-MM-DD HH:mm:ss'))
      }
    },
    {
      align: 'center',
      width: 280,
      title: '操作',
      render: (h, params) => {
        const row = { ...params.row }
        return filterHandle(vm, h, row)
      }
    }
  ]
}

const filterHandle = (vm, h, row) => {
  const rname = vm.$store.getters.userInfo.rname
  const demo = rname === 'ROLE_DEMO' ? true : false
  const { networkType } = row

  const hotButton = h(
    'Button',
    {
      props: {
        type: 'primary',
        size: 'small',
        shape: 'circle',
        disabled:
          demo || networkType === 1 || row.company == '长沙平和堂五一广场店'
      },
      style: {
        marginRight: '5px'
      },
      on: {
        click: () => {
          vm.$router.push({
            path: '/user/video/device/distribute/' + row.id
          })
        }
      }
    },
    '热力分布'
  )

  const videoButton = h(
    'Button',
    {
      props: {
        type: 'success',
        size: 'small',
        shape: 'circle',
        disabled:
          demo || networkType === 1 || row.company == '长沙平和堂五一广场店'
      },
      style: {
        marginRight: '5px'
      },
      on: {
        click: () => {
          vm.$router.push({
            path: '/user/video/device/player/' + row.id
          })
        }
      }
    },
    '视频图像'
  )

  let buttonArr = [
    h(
      'Button',
      {
        props: {
          type: 'info',
          size: 'small',
          shape: 'circle',
          disabled:
            demo || networkType === 1 || row.company == '长沙平和堂五一广场店'
        },
        style: {
          marginRight: '5px'
        },
        on: {
          click: () => {
            vm.$router.push({
              path: '/user/video/detail/' + row.id
            })
          }
        }
      },
      '数据详情'
    )
  ]

  if (row.videoStatus) {
    buttonArr.unshift(videoButton)
  }
  if (row.heatStatus) {
    buttonArr.unshift(hotButton)
  }

  return h('div', buttonArr)
}

const filterStatus = (h, row) => {
  let { status } = row
  let stateObject = {
    1: h(
      'Tag',
      {
        props: {
          color: 'green'
        }
      },
      '正常'
    ),
    2: h(
      'Tag',
      {
        props: {
          color: 'yellow'
        }
      },
      '告警'
    )
  }
  if (!(status in stateObject)) {
    return h(
      'Tag',
      {
        props: {
          color: '#909399'
        }
      },
      '未知'
    )
  }
  return stateObject[status]
}

const filterOnline = (h, row) => {
  const { online } = row
  if (online)
    return h(
      'Tag',
      {
        props: {
          color: 'green'
        }
      },
      '在线'
    )
  else
    return h(
      'Tag',
      {
        props: {
          color: 'red'
        }
      },
      '离线'
    )
}

//  <Button :size="buttonSize" icon="ios-download-outline" type="primary" shape="circle"></Button>
